package com.gzhryc.shared_device.oem.h5.agent;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.json.JsonTools;
import com.gzhryc.common.redis.RedisTools;
import com.gzhryc.common.secruity.EncryptTools;
import com.gzhryc.shared_device.oem.code.RedisDBConstants;
import com.gzhryc.weixin.gzh.msg.Oauth2WeixinUser;

public class WxUserCacheManager {

    static WxUserCacheManager me;

    public static WxUserCacheManager self() {
        if (me == null) {
            me = new WxUserCacheManager();
        }
        return me;
    }

    private Long times = 300L;

    public void save(String appId,String code, Oauth2WeixinUser oauth2WxUser){
        if(StringTools.isNotBlank(appId) && StringTools.isNotBlank(code) && oauth2WxUser != null) {
            String key = EncryptTools.md5(appId + code);
            String json = JsonTools.toJson(oauth2WxUser);
            RedisTools.get().set(RedisDBConstants.API_SESSION_DB, key, json, times);
        }
    }

    public Oauth2WeixinUser get(String appId,String code){
        if(StringTools.isNotBlank(appId) && StringTools.isNotBlank(code)){
            String key = EncryptTools.md5(appId + code);
            String json = RedisTools.get().get(RedisDBConstants.API_SESSION_DB,key);
            if(StringTools.isNotBlank(json)){
                return JsonTools.fromJson(json,Oauth2WeixinUser.class);
            }
        }
        return null;
    }
}
